package artid.com.arttogo.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.StrericWheelAdapter;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.view.OnWheelChangedListener;
import artid.com.arttogo.view.WheelView;


/**
 * TODO<请描述这个类是干什么的>
 *
 * @author 熊彬
 * @data: 2016-5-18 下午1:50:01
 * @version: V1.0
 */
public class AlertUtils {
    public static AlertDialog alertDialog;

    /**
     * 取消弹窗
     */
    public static void dismiss() {
        // TODO Auto-generated method stub
        alertDialog.dismiss();
        alertDialog = null;
    }


    public static AlertDialog initHeadAlertDialog(final Context mContext, int width, OnClickListener onClick) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.pop_menu_photo, null);

        view.findViewById(R.id.tv_phone).setOnClickListener(onClick);
        view.findViewById(R.id.tv_choice).setOnClickListener(onClick);
        view.findViewById(R.id.tv_cancel).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        dismiss();
                    }
                });

        alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.show();
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();

        params.width = width;

        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;

        Window window = alertDialog.getWindow();
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);
        return alertDialog;
    }

    /**
     * 选择图片对话框
     */
    static TextView textman;
    static TextView textwoman;
    public static Boolean isman = true;

    public static AlertDialog initPhotoAlertDialog(final Context mContext, OnClickListener onClick) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.pop_menu_sex, null);
        textman = (TextView) view.findViewById(R.id.text_pop_man);
        textwoman = (TextView) view.findViewById(R.id.text_pop_woman);
        view.findViewById(R.id.text_pop_ok).setOnClickListener(onClick);
        textman.setOnClickListener(
                new OnClickListener() {

                    @SuppressLint("NewApi")
                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        textman.setBackground(mContext.getResources().getDrawable(R.drawable.round_orange));
                        textman.setTextColor(Color.parseColor("#ffffff"));
                        textwoman.setBackground(mContext.getResources().getDrawable(R.drawable.stoke_orange));
                        textwoman.setTextColor(Color.parseColor("#f6833b"));
                        isman = true;
                    }
                });
        textwoman.setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        textwoman.setBackground(mContext.getResources().getDrawable(R.drawable.round_orange));
                        textwoman.setTextColor(Color.parseColor("#ffffff"));
                        textman.setBackground(mContext.getResources().getDrawable(R.drawable.stoke_orange));
                        textman.setTextColor(Color.parseColor("#f6833b"));
                        isman = false;
                    }
                });
        view.findViewById(R.id.text_pop_cancle).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        dismiss();
                    }
                });

        alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.show();
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        WindowManager wm = (WindowManager) mContext.getSystemService(
                Context.WINDOW_SERVICE);

        int width = wm.getDefaultDisplay().getWidth();
        params.width = width;

        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;

        Window window = alertDialog.getWindow();
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);
        return alertDialog;
    }

    private static TextView contentView;

    /**
     * 上传备案对话框
     */

    public static void setText(String content) {
        if (contentView != null) {
            contentView.setText(content);
        }
    }

    /**
     * 显示资讯发布须知
     */
    public static AlertDialog showConsultaDialog(final Context mContext,
                                                 int width, OnClickListener onClick) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.pop_menu_consintro, null);
        View img = (View) view.findViewById(R.id.dialog_mainimg);
        img.setOnClickListener(onClick);
        alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.show();
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        params.width = width;
        params.height = LayoutParams.WRAP_CONTENT;
        Window window = alertDialog.getWindow();
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);
        return alertDialog;
    }

    /**
     * 消息对话框
     */
    @SuppressLint("InflateParams")
    public static AlertDialog MessageAlertDialog(final Context mContext,
                                                 String title, int width, OnClickListener onClick) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.pop_menu_edit, null);
        TextView title_tv = (TextView) view.findViewById(R.id.tv_phone);
        title_tv.setText(title);
        view.findViewById(R.id.tv_ok).setOnClickListener(onClick);
        view.findViewById(R.id.tv_no).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        dismiss();
                    }
                });
        alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.show();
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        params.width = width * 4 / 5;

        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;

        Window window = alertDialog.getWindow();
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);
        return alertDialog;
    }
    /**
     * 拨打电话对话框
     */
    @SuppressLint("InflateParams")
    public static AlertDialog phoneAlertDialog(Activity mContext,
                                                 String title, OnClickListener onClick) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.pop_menu_phone, null);
        TextView title_tv = (TextView) view.findViewById(R.id.tv_phone);
        title_tv.setText(title);
        view.findViewById(R.id.tv_ok).setOnClickListener(onClick);
        view.findViewById(R.id.tv_no).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        dismiss();
                    }
                });
        alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.show();
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        int width = Integer.valueOf(NetUtils.getDisplayMetrics(mContext).get(NetUtils.DM_WIDTH));
        params.width = width * 4 / 5;

        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;

        Window window = alertDialog.getWindow();
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);
        return alertDialog;
    }


    @SuppressLint("InflateParams")
    public static AlertDialog scanAlertDialog(final Context mContext, final String imgurl,
                                              final String x, String y, final String info, int width, OnClickListener listener) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.dialog_scan_results, null);
        ImageView img = (ImageView) view.findViewById(R.id.dialog_scanResults_img);
//		Glide.with(mContext).load(imgurl+AppUtil.picWidthUrl)
//		.placeholder(R.drawable.ic_menu_rotate)
//		.error(R.drawable.ic_menu_rotate).into(img);
        Glide.with(mContext).load(imgurl + AppUtil.picWidthUrl).asBitmap().centerCrop()
                .placeholder(R.drawable.ic_menu_rotate).error(R.drawable.ic_menu_rotate)
                .into(new MyBitmapImageViewTarget(img));
//		TextView mx = (TextView) view.findViewById(R.id.dialog_scanResults_XCoordinates);
//		mx.setText(x);
//		TextView my = (TextView) view.findViewById(R.id.dialog_scanResults_YCoordinates);
//		my.setText(y);
        TextView mInfo = (TextView) view.findViewById(R.id.dialog_scanResults_remarks);
        mInfo.setText(info);
        mInfo.setMovementMethod(new ScrollingMovementMethod());
        ImageView mViewMicro = (ImageView) view.findViewById(R.id.dialog_show);
        mViewMicro.setOnClickListener(listener);

        alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.setCancelable(true);
        alertDialog.show();
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        params.width = width * 14 / 15;

        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;

        Window window = alertDialog.getWindow();
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);
        return alertDialog;
    }

    /**
     * 消息对话框
     */
    @SuppressLint("InflateParams")
    public static AlertDialog OneButtonAlertDialog(final Context mContext,
                                                   String Content, int width, OnClickListener onClick) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.pop_menu_onebutton, null);
        TextView title_tv = (TextView) view.findViewById(R.id.tv_content);
        title_tv.setText(Content);
        view.findViewById(R.id.tv_ok).setOnClickListener(onClick);
        alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.show();
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        params.width = width * 4 / 5;

        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;

        Window window = alertDialog.getWindow();
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);
        return alertDialog;
    }

    public static String pId = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35";
    public static String[] pids = pId.split(",");
    public static String PName = "北京,天津,河北,山西,内蒙古,辽宁,吉林,黑龙江,上海,江苏,浙江,安徽,福建,江西,山东,河南,湖北,湖南,广东,广西,海南,重庆,四川,贵州,云南,西藏,陕西,甘肃,青海,宁夏,新疆,台湾,香港,澳门,海外";
    public static String[] pnames = PName.split(",");
    //通过市找到省
    public static String CtoP = "1,9,2,22,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,20,20,20,20,20,20,20,20,20,20,20,20,20,20,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,24,24,24,24,24,24,24,24,24,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,26,26,26,26,26,26,26,27,27,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,28,28,28,28,28,28,28,29,29,29,29,29,29,29,29,30,30,30,30,30,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,34,35";
    public static String[] cpids = CtoP.split(",");
    public static String CName = "北京市,上海市,天津市,重庆市,石家庄市,唐山市,秦皇岛市,邯郸市,邢台市,保定市,张家口市,承德市,衡水市,廊坊市,沧州市,太原市,大同市,阳泉市,长治市,晋城市,朔州市,晋中市,运城市,忻州市,临汾市,吕梁市,呼和浩特市,包头市,乌海市,赤峰市,通辽市,鄂尔多斯市,呼伦贝尔市,巴彦淖尔市,乌兰察布市,兴安盟,锡林郭勒盟,阿拉善盟,沈阳市,大连市,鞍山市,抚顺市,本溪市,丹东市,锦州市,营口市,阜新市,辽阳市,盘锦市,铁岭市,朝阳市,葫芦岛市,长春市,吉林市,四平市,辽源市,通化市,白山市,松原市,白城市,延边朝鲜族自治州,哈尔滨市,齐齐哈尔市,鸡西市,鹤岗市,双鸭山市,大庆市,伊春市,佳木斯市,七台河市,牡丹江市,黑河市,绥化市,大兴安岭地区,南京市,无锡市,徐州市,常州市,苏州市,南通市,连云港市,淮安市,盐城市,扬州市,镇江市,泰州市,宿迁市,杭州市,宁波市,温州市,嘉兴市,湖州市,绍兴市,舟山市,衢州市,金华市,台州市,丽水市,合肥市,芜湖市,蚌埠市,淮南市,马鞍山市,淮北市,铜陵市,安庆市,黄山市,滁州市,阜阳市,宿州市,巢湖市,六安市,亳州市,池州市,宣城市,福州市,厦门市,莆田市,三明市,泉州市,漳州市,南平市,龙岩市,宁德市,南昌市,景德镇市,萍乡市,九江市,新余市,鹰潭市,赣州市,吉安市,宜春市,抚州市,上饶市,济南市,青岛市,淄博市,枣庄市,东营市,烟台市,潍坊市,济宁市,泰安市,威海市,日照市,莱芜市,临沂市,德州市,聊城市,滨州市,菏泽市,郑州市,开封市,洛阳市,平顶山市,安阳市,鹤壁市,新乡市,焦作市,濮阳市,许昌市,漯河市,三门峡市,南阳市,商丘市,信阳市,周口市,驻马店市,济源市,武汉市,黄石市,十堰市,宜昌市,襄樊市,鄂州市,荆门市,孝感市,荆州市,黄冈市,咸宁市,随州市,恩施土家族苗族自治州,仙桃市,潜江市,天门市,神农架林区,长沙市,株洲市,湘潭市,衡阳市,邵阳市,岳阳市,常德市,张家界市,益阳市,郴州市,永州市,怀化市,娄底市,湘西土家族苗族自治州,广州市,韶关市,深圳市,珠海市,汕头市,佛山市,江门市,湛江市,茂名市,肇庆市,惠州市,梅州市,汕尾市,河源市,阳江市,清远市,东莞市,中山市,潮州市,揭阳市,云浮市,南宁市,柳州市,桂林市,梧州市,北海市,防城港市,钦州市,贵港市,玉林市,百色市,贺州市,河池市,来宾市,崇左市,海口市,三亚市,五指山市,琼海市,儋州市,文昌市,万宁市,东方市,定安县,屯昌县,澄迈县,临高县,白沙黎族自治县,昌江黎族自治县,乐东黎族自治县,陵水黎族自治县,保亭黎族苗族自治县,琼中黎族苗族自治县,西沙群岛,南沙群岛,中沙群岛的岛礁及其海域,成都市,自贡市,攀枝花市,泸州市,德阳市,绵阳市,广元市,遂宁市,内江市,乐山市,南充市,眉山市,宜宾市,广安市,达州市,雅安市,巴中市,资阳市,阿坝藏族羌族自治州,甘孜藏族自治州,凉山彝族自治州,贵阳市,六盘水市,遵义市,安顺市,铜仁地区,黔西南布依族苗族自治州,毕节地区,黔东南苗族侗族自治州,黔南布依族苗族自治州,昆明市,曲靖市,玉溪市,保山市,昭通市,丽江市,思茅市,临沧市,楚雄彝族自治州,红河哈尼族彝族自治州,文山壮族苗族自治州,西双版纳傣族自治州,大理白族自治州,德宏傣族景颇族自治州,怒江傈僳族自治州,迪庆藏族自治州,拉萨市,昌都地区,山南地区,日喀则地区,那曲地区,阿里地区,林芝地区,西安市,铜川市,宝鸡市,咸阳市,渭南市,延安市,汉中市,榆林市,安康市,商洛市,兰州市,嘉峪关市,金昌市,白银市,天水市,武威市,张掖市,平凉市,酒泉市,庆阳市,定西市,陇南市,临夏回族自治州,甘南藏族自治州,西宁市,海东地区,海北藏族自治州,黄南藏族自治州,海南藏族自治州,果洛藏族自治州,玉树藏族自治州,海西蒙古族藏族自治州,银川市,石嘴山市,吴忠市,固原市,中卫市,乌鲁木齐市,克拉玛依市,吐鲁番地区,哈密地区,昌吉回族自治州,博尔塔拉蒙古自治州,巴音郭楞蒙古自治州,阿克苏地区,克孜勒苏柯尔克孜自治州,喀什地区,和田地区,伊犁哈萨克自治州,塔城地区,阿勒泰地区,石河子市,阿拉尔市,图木舒克市,五家渠市,台北市,高雄市,基隆市,台中市,台南市,新竹市,嘉义市,台北县,宜兰县,桃园县,新竹县,苗栗县,台中县,彰化县,南投县,云林县,嘉义县,台南县,高雄县,屏东县,澎湖县,台东县,花莲县,中西区,东区,九龙城区,观塘区,南区,深水埗区,黄大仙区,湾仔区,油尖旺区,离岛区,葵青区,北区,西贡区,沙田区,屯门区,大埔区,荃湾区,元朗区,澳门特别行政区,海外";
    public static String[] cnames = CName.split(",");
    public static String Cid = "36,39,40,62,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530,531,532,533,534,45055";
    public static String[] cids = Cid.split(",");
    public static int selectP = 0;
    public static int selectC = 0;
    public static String[] cids_Selected;

    public static AlertDialog CityDialog(Context mContext, OnClickListener onClick) {
        selectP=0;
        selectC=0;
        View view = LayoutInflater.from(mContext).inflate(R.layout.pop_second_level, null);
        WheelView wheelView1 = (WheelView) view.findViewById(R.id.pop_second_level_wheelView1);
        final WheelView wheelView2 = (WheelView) view.findViewById(R.id.pop_second_level_wheelView2);
        Button btnCancel = (Button) view.findViewById(R.id.pop_second_level_btnCancel);
        Button btnConfirm = (Button) view.findViewById(R.id.pop_second_level_btnConfirm);
        wheelView1.setAdapter(new StrericWheelAdapter(pnames));
        wheelView1.setCurrentItem(0);
        wheelView1.setCyclic(true);
        wheelView1.setInterpolator(new AnticipateOvershootInterpolator());
        wheelView1.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                // TODO Auto-generated method stub
                wheelView2.setAdapter(new StrericWheelAdapter(getCitys(newValue)));
                wheelView2.setCurrentItem(0);
                selectP = newValue;
            }
        });
        wheelView2.setAdapter(new StrericWheelAdapter(getCitys(wheelView1.getCurrentItem())));
        wheelView2.setCurrentItem(0);
        wheelView2.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                // TODO Auto-generated method stub
                selectC = newValue;
            }
        });
        btnCancel.setOnClickListener(onClick);
        btnConfirm.setOnClickListener(onClick);
        alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.show();
        WindowManager.LayoutParams params = alertDialog.getWindow()
                .getAttributes();
        params.width = Integer.parseInt(NetUtils.getDisplayMetrics((Activity) mContext).get(NetUtils.DM_WIDTH));

        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;

        Window window = alertDialog.getWindow();
        window.setAttributes(params);
        window.setContentView(view);
        alertDialog.getWindow().setContentView(view);
        return alertDialog;
    }

    static String[] getCitys(int PIndex) {
        String strall = "";
        String pid = pids[PIndex];
        String cidall = "";
        //找出对应城市id所在的index
        for (int i = 0; i < cpids.length; i++) {
            String pcid = cpids[i];
            if (pcid.equals(pid)) {
                strall += cnames[i] + ",";
                cidall += cids[i] + ",";
            }
        }
        cids_Selected = cidall.split(",");
        return strall.split(",");
    }

    public static String getPid() {
        return pids[selectP];
    }

    public static String getCid() {
        return cids_Selected[selectC];
    }

    public static String getCityName() {
        return getCitys(selectP)[selectC];
    }

    public static String getProvName() {
        return pnames[selectP];
    }
}
